﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace A2.ERP.Common
{
    public class LogHepler
    {
        //实例化一个队列
        public static Queue<string> ExcpectInfoQueue = new Queue<string>();

        //提供路径
        public static string LogBasePath;

        //通过线程实现
        static LogHepler()
        {
            ThreadPool.QueueUserWorkItem(o =>
            {
                while (true)
                {
                    if (ExcpectInfoQueue.Count > 0)
                    {
                        string str = ExcpectInfoQueue.Dequeue();

                        //StringBuilder str = new StringBuilder();

                        //str.Append("--------------------------------------");
                  

                        string strfilename = DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

                        string absolurtefilename = Path.Combine(LogBasePath, strfilename);

                        using (FileStream fs = new FileStream(absolurtefilename, FileMode.Append, FileAccess.Write))
                        {
                            byte[] data = Encoding.Default.GetBytes(str);

                            fs.Write(data, 0, data.Length);
                        }
                    }
                }
            });
        }
    }
}
